#include "item.h"
#include <QTime>


Item::Item(ITEM_TYPE t,int nShape) //类型和形状构造
{
    mPos=QPoint(0,0);    // 初始化方块类型的原点坐标

    InitItem(t,nShape);  // 根据方块类形的种类及形状，初始化方块类形的坐标

}

void Item::InitNew(int nSeed)
{

    if(nSeed == 0)
    {
        //如果没有随机因子，就使用当前时间作随机因子
        qsrand(QTime::currentTime().msec());  //qsrand, 使用Seed 生成随机数。后面的Qrand 使用相同的随机数序列
    }
    else
    {
        //传入随机因子
        qsrand(nSeed);
    }

    ITEM_TYPE t = (ITEM_TYPE)(qrand()%ITEM_MAX); // 产生随机的方块类型

    int s = qrand()%4; // 产生随机的形状
    //qDebug()<<"t="<<t<<"s="<<s;
    InitItem(t,s); //构造方块类型的坐标
}

void Item::InitItem(ITEM_TYPE t,int nShape)   //根据方块类形的种类及形状，初始化方块类形的坐标
{
    mPoints.clear();  //坐标初始化

    mType = t;       //初始化Item类体变量mType
    mShape = nShape;  //初始化Item类体变量mShape
    switch (t)
    {
    case ITEM_1:   //如果是长条形
    {
        if (nShape%2 == 0) //如果形状是0或2
        {
            for (int i = 0; i < 4; i++)
            {
                mPoints.append(mPos + QPoint( i,2));//QPoint 加法重载 ，(0,2),(1,2),(2,2),(3,2)
            }
        }
        else if (nShape%2 == 1) //如果形状是1或3
        {
            for (int i = 0; i < 4; i++)
            {
                mPoints.append(mPos + QPoint( 2,i));//(2,0),(2,1),(2,2),(2,3)
            }
        }
        break;
    }
    case ITEM_2: // 如果是山字形
    {
        if (nShape == 0)  //如果形状是0
        {
            mPoints.append(mPos + QPoint( 1,0));
            mPoints.append(mPos + QPoint( 0,1));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
        }
        else if (nShape == 1)  //如果形状是1
        {
            mPoints.append(mPos + QPoint( 1,0));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
        }
        else if (nShape == 2) //如果形状是2
        {
            mPoints.append(mPos + QPoint( 0,1));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
        }
        else if (nShape == 3) //如果形状是3
        {
            mPoints.append(mPos + QPoint( 1,0));
            mPoints.append(mPos + QPoint( 0,1));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 1,2));
        }

        break;
    }
    case ITEM_3: // 手枪形1
    {
        if (nShape == 0)
        {
            mPoints.append(mPos + QPoint( 1,0));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 2,2));
        }
        else if (nShape == 1)
        {
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 3,1));
            mPoints.append(mPos + QPoint( 1,2));
        }
        else if (nShape == 2)
        {
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 2,2));
            mPoints.append(mPos + QPoint( 2,3));
        }
        else if (nShape == 3)
        {
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 2,2));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 0,2));
        }
        break;
    }
    case ITEM_4:  // 手枪形2
    {
        if (nShape == 0)
        {
            mPoints.append(mPos + QPoint( 2,0));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 2,2));
            mPoints.append(mPos + QPoint( 1,2));
        }
        else if (nShape == 1)
        {
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 2,2));
            mPoints.append(mPos + QPoint( 3,2));
        }
        else if (nShape == 2)
        {
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 1,3));
        }
        else if (nShape == 3)
        {
            mPoints.append(mPos + QPoint( 0,1));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 2,2));
        }
        break;
    }
    case ITEM_5://田字形
    {
        mPoints.append(mPos + QPoint( 0,0));
        mPoints.append(mPos + QPoint( 0,1));
        mPoints.append(mPos + QPoint( 1,0));
        mPoints.append(mPos + QPoint( 1,1));
        break;
    }
    case ITEM_6: //s字形
    {
        if (nShape %2==0)
        {
            mPoints.append(mPos + QPoint( 1,0));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 2,2));
        }

        else if (nShape %2==1)
        {
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 0,2));
            mPoints.append(mPos + QPoint( 1,2));
        }

        break;
    }
    case ITEM_7: //z字形
    {
        if (nShape %2==0)
        {
            mPoints.append(mPos + QPoint( 2,0));
            mPoints.append(mPos + QPoint( 2,1));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 1,2));
        }
        else if (nShape %2==1)
        {
            mPoints.append(mPos + QPoint( 0,1));
            mPoints.append(mPos + QPoint( 1,1));
            mPoints.append(mPos + QPoint( 1,2));
            mPoints.append(mPos + QPoint( 2,2));
        }

        break;

    }
    }
}

void Item::ChangeShape(int nAdd)  //改变方块类型形状
{
    mShape=(mShape+nAdd)%4;  //重新产生方块类形形状态
    InitItem(mType,mShape);  //更新方块类型坐标
}

void Item::AddPoints(QVector<QPoint> &points)  //添加方块格子坐标。size, contains, append 者是Vector 类的函数。
{
    for(int i=0;i<points.size();i++)  // 遍历Points
    {
        if(!mPoints.contains(points[i]))  //如果mPoints中没有
            mPoints.append(points[i]);   //添加进mPoints
    }
}

void Item::Move(int x, int y)    //横向移动x格,竖向移动y格
{
    for(int i=0;i<mPoints.size();i++)  //遍历mPoints ( 方块类形坐标容噐）
    {
        int x1=mPoints[i].x()+x;   //横坐标加x;
        int y1=mPoints[i].y()+y;   //横坐标加y;
        mPoints[i].setX(x1);       //新的x回写
        mPoints[i].setY(y1);      //新的y回写
    }
    mPos=mPos+QPoint(x,y);      //原点位置回写
}

void Item::MoveTo(int x, int y)       //移动到位置(x,y)，即原点移动到（x,y)
{
    for(int i=0;i<mPoints.size();i++)   //遍历mPoints ( 方块类形坐标容噐）
    {
        int x1=mPoints[i].x()+(x-mPos.x());  //移动后的x坐标;
        int y1=mPoints[i].y()+(y-mPos.y());  //移动后的y坐标;
        mPoints[i].setX(x1);                 //x回写
        mPoints[i].setY(y1);                 //y回写
    }
    mPos=QPoint(x,y);      //原点位置回写
}

void Item::MoveDown(int nRow, int y)  //第nRow行以上的部分下移y行，用在消格之后
{
    for(int i=0;i<mPoints.size();i++)    //遍历mPoints ( 方块类形坐标容噐）
    {
        if(mPoints[i].y()<nRow)          //行坐标小于nRow 的
            mPoints[i].setY(mPoints[i].y()+y); //行标加y，后回写
    }
}

void Item::DeleteRow(int y)   //删除第y行
{
    QVector<QPoint> newPoints;   //新建一个新的容器
    for(int i=0;i<mPoints.size();i++)  //遍历mPoints ( 方块类形坐标容噐）
    {
        if(mPoints[i].y()!=y)            //如果行坐标不等于y,
            newPoints.append(mPoints[i]); //添加到新的点容器

    }
    mPoints=newPoints;  //新的容器回写到mPoints. （Vector 类=号重载）
}

// void Item::DeleteCurrentItem(){

// }

void Item::Draw(QPainter &painter, int nStartX, int nStartY, int nW, int nH)  //根据方块格子的起始位置座标，长，宽，画出方块格子
{
    for (int i = 0; i< mPoints.size(); i++)   //遍历mPoints
    {
        QPoint pt = mPoints[i];
        //drawRect :画正方形，并填充
        painter.drawRect(QRect(nStartX + pt.x() * nW,nStartY + pt.y() * nH,nW,nH)); //QRect( X,Y,width,height), 构造一个以(x,y) 为左上角, 长为width, 高为height 的长方形
    }
}
